package homes;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import items.BrandDTO;
import items.Stockable;

@SuppressWarnings("serial")
public class BrandDTOHome extends GenericHome<BrandDTO>{

	@Override
	protected Class<BrandDTO> getDomainClass() {
		return BrandDTO.class;
	}
	
	
	//--------------BrandDTO Methods
	
	public void save(BrandDTO brand) {
		this.getHibernateTemplate().save(brand);
	    this.getHibernateTemplate().flush();
	}

	/** Devuelve todas las marcas **/
	public List<String> getBrands() {
	     return this.getHibernateTemplate().execute(new HibernateCallback<List<String>>() {
	            public List<String> doInHibernate(final Session session) throws HibernateException, SQLException {
	            	  Query query = session
	                          .createQuery("SELECT DISTINCT b.brand FROM BrandDTO as b");
	                  return query.list();
	            }
	        });
	}
	
	/** Devuelve los modelos de una marca **/
	public List<String> getModelsOfMark(final String mark) {
	     return this.getHibernateTemplate().execute(new HibernateCallback<List<String>>() {
	            public List<String> doInHibernate(final Session session) throws HibernateException, SQLException {
	            	  Query query = session
	                          .createQuery("SELECT DISTINCT b.model FROM BrandDTO as b Where b.brand = :marca");
	            	  query.setString("marca", mark);
	                  return query.list();
	            }
	        });
	}

	/** Devuelve las marcas y modelos para una categoria **/
	public List<BrandDTO> getBrandsAndModels(final String type) {
		   return this.getHibernateTemplate().execute(new HibernateCallback<List<BrandDTO>>() {
	            public List<BrandDTO> doInHibernate(final Session session) throws HibernateException, SQLException {
	                Criteria criteria = session.createCriteria(BrandDTO.class);
	                criteria.add(Restrictions.eq("category", type));
	                criteria.addOrder( Order.asc("brand") );
	                return (List<BrandDTO>) criteria.list();
	            }
	        });
	}

    /** Devuelve las marcas de los neumaticos **/
	public List<String> getTireBrands() {
	     return this.getHibernateTemplate().execute(new HibernateCallback<List<String>>() {
	            public List<String> doInHibernate(final Session session) throws HibernateException, SQLException {
	            	  Query query = session
	                          .createQuery("SELECT DISTINCT b.brand FROM BrandDTO as b WHERE b.category = :tire");
	            	  query.setString("tire", "N");
	                  return query.list();
	            }
	        });
	}
	
	/** Devuelve las marcas de los autos **/
	public List<String> getCarBrands() {
	     return this.getHibernateTemplate().execute(new HibernateCallback<List<String>>() {
	            public List<String> doInHibernate(final Session session) throws HibernateException, SQLException {
	            	  Query query = session
	                          .createQuery("SELECT DISTINCT b.brand FROM BrandDTO as b WHERE b.category = :car");
	            	  query.setString("car", "A");
	                  return query.list();
	            }
	        });
	}

}
